package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.system.domain.RequestLog;
import com.ruoyi.system.domain.vo.RequestLogPageInfo;
import com.ruoyi.system.mapper.RequestLogMapper;
import com.ruoyi.system.service.RequestLogService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RequestLogServiceImpl extends ServiceImpl<RequestLogMapper, RequestLog> implements RequestLogService {
    @Override
    public List<RequestLog> getRequestlogPage(RequestLogPageInfo requestLogPageInfo, Integer pageNum, Integer pageSize)  {
        LambdaQueryWrapper<RequestLog>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(requestLogPageInfo.getIp())){
            lambdaQueryWrapper.eq(RequestLog::getIp,requestLogPageInfo.getIp());
        }
        if(StringUtils.isNotBlank(requestLogPageInfo.getUrl())){
            lambdaQueryWrapper.eq(RequestLog::getUrl,requestLogPageInfo.getUrl());
        }
        if(StringUtils.isNotBlank(requestLogPageInfo.getMethod())){
            lambdaQueryWrapper.eq(RequestLog::getMethod,requestLogPageInfo.getMethod());
        }
        if(requestLogPageInfo.getTimestampEnd()!=null){
            lambdaQueryWrapper.ge(RequestLog::getTimestampStart,requestLogPageInfo.getTimestampStart());
        }
        if(requestLogPageInfo.getTimestampStart()!=null){
            lambdaQueryWrapper.le(RequestLog::getTimestampEnd,requestLogPageInfo.getTimestampEnd());
        }
        if(requestLogPageInfo.getStatus()!=null){
            lambdaQueryWrapper.eq(RequestLog::getStatus,requestLogPageInfo.getStatus());
        }
        Page<RequestLog> page=baseMapper.selectPage(new Page<>(pageNum,pageSize),lambdaQueryWrapper);
        List<RequestLog> records=page.getRecords();
        return records;
    }

    @Override
    public Long getRequestlogCount(RequestLogPageInfo requestLogPageInfo) {
        LambdaQueryWrapper<RequestLog>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(requestLogPageInfo.getIp())){
            lambdaQueryWrapper.eq(RequestLog::getIp,requestLogPageInfo.getIp());
        }
        if(StringUtils.isNotBlank(requestLogPageInfo.getUrl())){
            lambdaQueryWrapper.eq(RequestLog::getUrl,requestLogPageInfo.getUrl());
        }
        if(StringUtils.isNotBlank(requestLogPageInfo.getMethod())){
            lambdaQueryWrapper.eq(RequestLog::getMethod,requestLogPageInfo.getMethod());
        }
        if(requestLogPageInfo.getTimestampEnd()!=null){
            lambdaQueryWrapper.ge(RequestLog::getTimestampStart,requestLogPageInfo.getTimestampStart());
        }
        if(requestLogPageInfo.getTimestampStart()!=null){
            lambdaQueryWrapper.le(RequestLog::getTimestampEnd,requestLogPageInfo.getTimestampEnd());
        }
        if(requestLogPageInfo.getStatus()!=null){
            lambdaQueryWrapper.eq(RequestLog::getStatus,requestLogPageInfo.getStatus());
        }
        return baseMapper.selectCount(lambdaQueryWrapper);
    }
}
